package com.pipe.protocol.query;

import com.pipe.util.MysqlProtocolUtil;
import io.netty.buffer.ByteBuf;

/**
 * @author Kiven
 */
public class ColumnDefinition41Package {
    private String catalog;
    private String schema;
    private String table;
    private String orgTable;
    private String name;
    private String orgName;
    private short fixedFieldLength;
    private int characterSet;
    private long columnLength;
    private short type;
    private int flags;
    private short decimals;

    public ColumnDefinition41Package(String catalog, String schema, String table, String orgTable, String name,
                                     String orgName, short fixedFieldLength, int characterSet, long columnLength, short type, int flags, short decimals) {
        this.catalog = catalog;
        this.schema = schema;
        this.table = table;
        this.orgTable = orgTable;
        this.name = name;
        this.orgName = orgName;
        this.fixedFieldLength = fixedFieldLength;
        this.characterSet = characterSet;
        this.columnLength = columnLength;
        this.type = type;
        this.flags = flags;
        this.decimals = decimals;
    }

    public static ColumnDefinition41Package build(ByteBuf byteBuf) {
        String catalog = MysqlProtocolUtil.readLenencStringFromBytebuf(byteBuf);
        String schema = MysqlProtocolUtil.readLenencStringFromBytebuf(byteBuf);
        String table = MysqlProtocolUtil.readLenencStringFromBytebuf(byteBuf);
        String orgTable = MysqlProtocolUtil.readLenencStringFromBytebuf(byteBuf);
        String name = MysqlProtocolUtil.readLenencStringFromBytebuf(byteBuf);
        String orgName = MysqlProtocolUtil.readLenencStringFromBytebuf(byteBuf);
        short fixedFieldLength = byteBuf.readUnsignedByte();
        int characterSet = byteBuf.readUnsignedShortLE();
        long columnLength = byteBuf.readUnsignedIntLE();
        short type = byteBuf.readUnsignedByte();
        int flags = byteBuf.readUnsignedShortLE();
        short decimals = byteBuf.readUnsignedByte();
        return new ColumnDefinition41Package(catalog, schema, table, orgTable, name, orgName, fixedFieldLength, characterSet, columnLength, type, flags, decimals);
    }

    public boolean isInformationSchema(){
        return "information_schema".equals(this.schema);
    }

    public String getTable() {
        return table;
    }

    @Override
    public String toString() {
        return "ColumnDefinition41Package{" +
                "catalog='" + catalog + '\'' +
                ", schema='" + schema + '\'' +
                ", table='" + table + '\'' +
                ", orgTable='" + orgTable + '\'' +
                ", name='" + name + '\'' +
                ", orgName='" + orgName + '\'' +
                ", fixedFieldLength=" + fixedFieldLength +
                ", characterSet=" + characterSet +
                ", columnLength=" + columnLength +
                ", type=" + type +
                ", flags=" + flags +
                ", decimals=" + decimals +
                '}';
    }
}